<?php
// Only admin use this
class Windman extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('services/novel_service', 'novel_service');
        $this->load->model('services/essence_service', 'essence_service');
        $this->load->model('services/banner_service', 'banner_service');
        if($this->userinfo['role'] !== $this->user_model->role['admin'])
        {
            message_logger('error', 'no acccess');
            $this->renderJson(900, '权限错误');
        }
    }

    public function getNovelList()
    {
        $page = $this->request_array['page'] ?? 1;
        $page_size = $this->request_array['pageSize'] ?? 20;
        $res = $this->novel_service->getNovelLists($page, $page_size);
        $this->renderJson(STATUS_OK, $res);
    }

    public function createEssence()
    {
        $novel_ids = $this->request_array['novelIds'] ?? '';
        $name = $this->request_array['name'] ?? '';
        $type = $this->request_array['type'] ?? '';
        $err = null;
        $create = $this->essence_service->create($name, $type, $novel_ids, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson();
    }

    public function delete()
    {
        $id = $this->request_array['id'] ?? '';
        $this->essence_service->markDel($id);
        $this->renderJson();
    }

    public function createBanner()
    {
        $novel_id = $this->request_array['novelId'] ?? null;
        $url = $this->request_array['url'] ?? null;
        $err = null;
        $banner_data = [
            'novel_id'      => $novel_id,
            'url'           => $url,
        ];
        $create = $this->banner_service->createBanner($banner_data, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson();
    }

    public function deleteBanner()
    {
        $banner_id = $this->request_array['bannerId'] ?? null;
        $err = null;
        $this->banner_service->delete($banner_id, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson();
    }

    public function hideBanner()
    {
        $banner_id = $this->request_array['bannerId'] ?? null;
        $hide = $this->request_array['hide'] ?? null;
        $err = null;
        $this->banner_service->hideBanner($banner_id, $hide, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson();
    }

    public function bannerList()
    {
        $lists = $this->banner_service->getAllList();
        $this->renderJson(STATUS_OK, $lists);
    }
}